package com.greendeliver.activities;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.test.R;
import com.greendeliver.activities.base.BaseListActivity;
import com.greendeliver.business.EventManager;

public class AllEventsActivity extends BaseListActivity {

	private ArrayList<HashMap<String, String>>	eventsList;

	private ProgressDialog						pDialog;

	private static final String					TAG_ID		= "ID_event";
	private static final String					TAG_NAME	= "name_event";

	@Override
	protected void onCreate( Bundle savedInstanceState ) {
		super.onCreate( savedInstanceState );
		setContentView( R.layout.activity_all_events );

		this.pDialog = new ProgressDialog( AllEventsActivity.this );
		eventsList = new ArrayList<HashMap<String, String>>();

		new Load().execute();

		ListView lv = getListView();

		// on seleting single product
		// launching Edit Product Screen
		lv.setOnItemClickListener( new OnItemClickListener() {

			@Override
			public void onItemClick( AdapterView<?> parent, View view,
					int position, long id ) {
				// getting values from selected ListItem
				String id_event = ( (TextView) view.findViewById( R.id.id_event ) ).getText().toString();

				// Starting new intent
				Intent in = new Intent( getApplicationContext(),
						EventDetailActivity.class );
				// sending id to next activity
				in.putExtra( TAG_ID, id_event );

				// starting new activity and expecting some response back
				startActivity( in );
			}
		} );

	}

	class Load extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute()
		{
			super.onPreExecute();
			pDialog.setMessage( "Chargement des Spectacles" );
			pDialog.setIndeterminate( false );
			pDialog.setCancelable( false );
			pDialog.show();
		}

		/**
		 * getting All products from url
		 * */
		@Override
		protected String doInBackground( String... args )
		{
			EventManager em = new EventManager();
			eventsList = em.get_all_events_list();
			Log.i( "dfdf", eventsList.toString() );
			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		@Override
		protected void onPostExecute( String file_url )
		{
			pDialog.dismiss();

			ListAdapter adapter = new SimpleAdapter( AllEventsActivity.this,
					eventsList, R.layout.list_item_spectacles,
					new String[] { TAG_ID, TAG_NAME },
					new int[] { R.id.id_event, R.id.name_event, } );
			setListAdapter( adapter );
		}
	}

}
